<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        文件上传
    </title>


    <link rel="stylesheet" type="text/css" href="__STATIC__/webuploader/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/webuploader/style.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/webuploader/webuploader.css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
</head>

<body style="">
    <div id="wrapper">
              <h3 style="text-align: center;">Problem ID:{$pid} 的文件列表</h3>
         <table class="layui-table">
            
            <tbody>
                {foreach $filelist as $v}
                    <tr>
                        <th style="width: 70%">
                            {$v.filename}
                        </th>
                        <th>
                            <a href="{:url('admin/Problem/del_test_data',['filename'=>urlencode($v.filename),'pid'=>$pid])}" onclick="return confirm('确定要删除吗？')" class="layui-btn layui-btn-danger">删除</a>
                            <a href="{:url('admin/Problem/download',['filename'=>urlencode($v.filename),'pid'=>$pid])}" class="layui-btn layui-btn-primary">下载</a>
                        </th>
                    </tr>
                {/foreach}
            </tbody>
        </table>

        <div class="page-container">


            <div id="uploader" class="wu-example">
                <!--用来存放文件信息-->
                <div id="thelist" class="uploader-list"></div>
                <div class="btns">
                    <div id="picker">选择文件</div>
                    <button id="ctlBtn" class="btn btn-default">开始上传</button>
                    <button onclick="location.reload()" class="btn btn-default">刷新一下</button>
                </div>
            </div>
            


        </div>


    </div>
    <script type="text/javascript">
        // 添加全局站点信息
        var BASE_URL = '/webuploader';
    </script>
    <script type="text/javascript" src="__STATIC__/webuploader/jquery-1.10.2.min.js"></script>

    <script type="text/javascript" src="__STATIC__/webuploader/webuploader.js"></script>

    <script>
        // 文件上传
jQuery(function() {
    var $ = jQuery,
        $list = $('#thelist'),
        $btn = $('#ctlBtn'),
        state = 'pending',
        uploader;

    uploader = WebUploader.create({

        // 不压缩image
        resize: false,

        // swf文件路径
        //swf: BASE_URL + '/js/Uploader.swf',

        // 文件接收服务端。
        server: '{:url("admin/Upload/upload_data",["pid"=>$pid])}',
        
        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#picker',
        chunked:true,
        chunkSize:1048576,
        accept:{
            title: 'Images',
            extensions: 'c,cc,cpp,in,out,md',
            mimeTypes: '*'
        }
    });

    // 当有文件添加进来的时候
    uploader.on( 'fileQueued', function( file ) {
        $list.append( '<div id="' + file.id + '" class="item">' +
            '<div class="layui-inline"><h4 class="info">' + file.name + '</h4></div>' +
            '<div class="layui-inline"><button class="remove-this-file layui-btn layui-btn-danger layui-btn-sm" id="'+file.id+'">取消上传</button>'+
            '</div><p class="state">等待上传...</p>' +
        '</div><hr />' );
    });

    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
              '<div class="progress-bar" role="progressbar" style="width: 0%">' +
              '</div>' +
            '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中');

        $percent.css( 'width', percentage * 100 + '%' );
    });
    
    uploader.on( 'uploadSuccess', function( file ) {
        $( '#'+file.id ).find('p.state').text('已上传');
    });

    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
    });

    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });
    
    
    //
    uploader.on( 'uploadSuccess', function() {
       // window.location.reload();
    });
    
    uploader.on( 'all', function( type ) {
        if ( type === 'startUpload' ) {
            state = 'uploading';
        } else if ( type === 'stopUpload' ) {
            state = 'paused';
        } else if ( type === 'uploadFinished' ) {
            state = 'done';
        }

        if ( state === 'uploading' ) {
            $btn.text('暂停上传');
        } else {
            $btn.text('开始上传');
        }
    });

    $btn.on( 'click', function() {
        if ( state === 'uploading' ) {
            uploader.stop();
        } else {
            uploader.upload();
        }
    });
    
    $list.on('click','.remove-this-file',function(){
        //alert(this.id);
        uploader.removeFile( this.id );
        $(this).parent().parent().remove();
    });
});
    </script>




</body>

</html>